﻿
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
    
    static Dictionary<string, string> users = new Dictionary<string, string>();

    static void Main(string[] args)
    {
        while (true)
        {
            Console.Clear();
            Console.WriteLine("===== 主菜单 =====");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("\n请选择功能(1-3): ");

            string choice = Console.ReadLine();
            
            
            
            if (!int.TryParse(choice, out int option))
            {
                Console.WriteLine("请输入有效数字");
                Pause();
                continue;
            }

            switch (option)
            {
                case 1:
                    Login();
                    break;
                case 2:
                    Register();
                    break;
                case 3:
                    Console.WriteLine("谢谢使用，再见！");
                    return;
                default:
                    Console.WriteLine("请输入1-3之间的数字");
                    Pause();
                    break;
            }
        }
    }

    
    
    
    static void Register()
    {
        Console.Clear();
        Console.WriteLine("===== 用户注册 =====");
        
        
        string username = "";
        while (true)
        {
            Console.Write("请输入用户名: ");
            username = Console.ReadLine()?.Trim() ?? "";
            
            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不可为空");
                continue;
            }
            
            if (username.Length < 3 || username.Length > 15)
            {
                Console.WriteLine("用户名长度必须为3-15个字符");
                continue;
            }
            
            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                Console.WriteLine("用户名只能包含字母、数字和下划线");
                continue;
            }
            
            
            if (users.ContainsKey(username))
            {
                Console.WriteLine("用户名已存在");
                continue;
            }

            break; 
        }
        
        string password = "";
        while (true)
        {
            Console.Write("请输入密码: ");
            password = Console.ReadLine()?.Trim() ?? "";
            
            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不可为空");
                continue;
            }

            
            if (password.Length < 6 || password.Length > 20)
            {
                Console.WriteLine("密码长度必须为6-20个字符");
                continue;
            }
            
            bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
            bool hasLower = Regex.IsMatch(password, @"[a-z]");
            bool hasNumber = Regex.IsMatch(password, @"[0-9]");

            if (!hasUpper || !hasLower || !hasNumber)
            {
                Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字");
                continue;
            }
            
            Console.Write("请确认密码: ");
            string confirmPwd = Console.ReadLine()?.Trim() ?? "";

            if (string.IsNullOrEmpty(confirmPwd))
            {
                Console.WriteLine("确认密码不可为空");
                continue;
            }

            if (password != confirmPwd)
            {
                Console.WriteLine("两次输入的密码不一致");
                continue;
            }

            break; 
        }
        
        users.Add(username, password);
        Console.WriteLine("注册成功！");
        Pause();
    }

   
    static void Login()
    {
        Console.Clear();
        Console.WriteLine("===== 用户登录 =====");

        
        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim() ?? "";

        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不可为空");
            Pause();
            return;
        }

        
        if (!users.ContainsKey(username))
        {
            Console.WriteLine("用户名不存在");
            Pause();
            return;
        }
        
        Console.Write("请输入密码: ");
        string password = Console.ReadLine()?.Trim() ?? "";

        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不可为空");
            Pause();
            return;
        }
        
        if (users[username] == password)
        {
            Console.WriteLine("登录成功！欢迎进入系统首页。");
        }
        else
        {
            Console.WriteLine("密码错误");
        }

        Pause();
    }

    
    static void Pause()
    {
        Console.WriteLine("\n按任意键继续...");
        Console.ReadKey();
    }
}
    